package com.wthink.newMain.settings.config.inner.userConfig

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentDataBinding
import com.wthink.hzgj.databinding.FragmentUserCongigBinding
import com.wthink.newMain.settings.data.viewmodel.DataViewmodel
import com.wthink.newMain.view.ChangePwdDialog

class UserConfigFragment : Fragment() {

    private val viewModel by viewModels<UserConfigViewmodel>()

    private lateinit var binding: FragmentUserCongigBinding

    private val changePwdDialog: ChangePwdDialog by lazy {
        ChangePwdDialog(activity!!)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_user_congig,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.getUsers()

        binding.btnEdit1.setOnClickListener {
            changePwdDialog.show()
            changePwdDialog.chose(1)
        }
        binding.btnEdit2.setOnClickListener {
            changePwdDialog.show()
            changePwdDialog.chose(2)
        }
        binding.btnEdit3.setOnClickListener {
            changePwdDialog.show()
            changePwdDialog.chose(3)
        }

        changePwdDialog.setEditCallBack(object : ChangePwdDialog.EditInterface {
            override fun editCallback(index: Int,admin:String,pwd:String) {
                viewModel.setUser(index,admin,pwd)
            }
        })
    }
}